/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package web.enity;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 10592
 */
public class Cart {
    private List<CartItem> items = new ArrayList<>();
    private UserInfo userInfo;

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }
    public double getCost(){
        double cost=0;
        for (CartItem item : items) {
            Book b=item.getBook();
            if (b!=null) {
                cost+=item.getPrice();
            }
        }
        return cost;
    }
    public void add(Book book) {
        CartItem ci = new CartItem();
        ci.setBook(book);
        if (items.contains(ci)) {
            CartItem item = items.get(items.indexOf(ci));
            item.add(1);
        } else {
            CartItem item = new CartItem();
            item.setBook(book);
            item.setNumber(1);
            items.add(item);
        }
    }
    public boolean delete(int bid) {
        // 查找到购物车条目
        for (CartItem item : items) {
            Book b = item.getBook();
            if (b != null && b.getBid()== bid) {
                return items.remove(item);
            }
        }
        return false;
    }
    public static void main(String[] args) {
        Cart cart=new Cart();
        Book b1=new Book(2, "111", 1.9, "11111", 2);
        Book b2=new Book(2, "111", 1.9, "11111", 2);
        cart.add(b2);
        cart.add(b1);
        for (CartItem item : cart.getItems()) {
            System.out.println(item.getBook().getBid()+"\t"+item.getNumber());
        }
    }
}
